<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: huajie <banhuajie@163.com>
// +----------------------------------------------------------------------

namespace Admin\Controller;

/**
 * 行为控制器
 * @author huajie <banhuajie@163.com>
 */
class WeixinController extends AdminController {

    /**
     * 行为日志列表
     * @author huajie <banhuajie@163.com>
     */
    protected function arraySearch($keyword,$array){
        foreach($array as $k => $v){// print_r($keyword);exit;
            if(strstr( $keyword,$v)){
                return true;break;
            }
        }
    }

    public function config(){

        //获取列表数据
        $map =array();
        $list   =   $this->lists('Weixinreply', $map);
        $this->assign('_list', $list);
        $this->meta_title = '行为日志';
        $this->display();
    }
    public function add(){

        if(IS_POST){
            $title      = I('post.title');
            $keywords   = I('post.keywords');
            $type       = I('post.type');
            $content    = I('post.content');
            $status     = I('post.status');
            if(empty($title)){
                $this->error('标题不能为空');
            }
            if($type<3){
                  if(empty($keywords)){
                      $this->error('关键字不能为空');
                  }
            }
            if(empty($content)){
                $this->error('回复内容不能为空');
            }
            $data = [
                'title'     =>$title,
                'keywords'  =>$keywords,
                'type'      =>$type,
                'content'   =>$content,
                'status'    =>$status,
            ];
            $model = M('Weixinreply');
            if($model->create($data)){
                $model->add($data);
                $this->success('添加成功！',U('config'));
            }else{
                $this->error('添加失败！');
            }
        }else{
            $this->meta_title = '添加';
            $this->display('edit');
        }
    }
    /**
     *  添加
     * @author huajie <banhuajie@163.com>
     */
    public function edit($id = 0){


        if(IS_POST){
            $title      = I('post.title');
            $keywords   = I('post.keywords');
            $type       = I('post.type');
            $content    = I('post.content');
            $status     = I('post.status');
            $id         = I('post.id');
            empty($id) && $this->error('参数错误！');
            if(empty($title)){
                $this->error('标题不能为空');
            }
            if($type<3){
                if(empty($keywords)){
                    $this->error('关键字不能为空');
                }
            }
            if(empty($content)){
                $this->error('回复内容不能为空');
            }
            $data = [
                'title'     =>$title,
                'keywords'  =>$keywords,
                'type'      =>$type,
                'content'   =>$content,
                'status'    =>$status,
            ];
            $model = M('Weixinreply');
            if($model->create($data)){
                $model->where('id='.$id)->save($data);
                $this->success('修改成功！',U('config'));
            }else{
                $this->error('修改失败！');
            }
        }else{
            empty($id) && $this->error('参数错误！');
            $info = M('Weixinreply')->field(true)->find($id);
            $this->assign('info', $info);
            $this->meta_title = '修改';
            $this->display();
        }
    }

}
